// pages/service/service.js
var app = getApp();
var util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      send_time:options.send_time
    })
    this.myOrderList();
  },
  //选择日期
  bindDateChange: function (e) {
    //   console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      currentPage: 1,
      orderlist: []
    })
    this.myOrderList();
  },
  //我的订单
  myOrderList: function () {
    var that = this;
    util.showLoading();
    console.log(that.data.send_time)
    var apiUrl = '/api/dis/order/search_order';
    var apiData = {
      send_time: that.data.send_time,
      keyword: that.data.keyword
    }
    util.fetchPost(apiUrl, apiData, function (res) {
      util.hideLoading();
      if (res.state == 1) {
        that.setData({
          orderlist: res.datas,
          ready: res.datas.length == 0 ? 1 : 2,
        });
      } else {
        util.message(res.datas);
      }
    });
  },
  searchInputTap: function (e) {
    var that=this;
    that.setData({
      keyword: e.detail.value,
      orderlist:[]
    })
    that.myOrderList()
  },

  //一键送达
  oneClickTap:function(){
	  var that = this;
	  var allid = [];
	  for (var i in that.data.orderlist) {
		  allid.push(that.data.orderlist[i].id)
	  }
	  var apiUrl = '/api/dis/order/one_btn_take_arrived';
	  var apiData = {
		  order_id_str: allid.join("|")
	  }
	  util.confirm("确认送达吗？", function (res) {
		  if (res.confirm) {
			  util.showLoading();
			  util.fetchPost(apiUrl, apiData, function (res) {
				  util.hideLoading();
				  if (res.state == 1) {
					  util.showSuccess("送达成功");
					  setTimeout(function(){
						  that.myOrderList()
					  },1000)
					  
				  } else {
					  util.message(res.datas);
				  }
			  });
			  
		  }

	  })




	
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})